use crate::tool::Tool;
use meval::eval_str;
use tool_macro::tool;
use serde_json::{json, Value};
use anyhow::{Context, Result};
use async_trait::async_trait;

#[tool(
    struct_name = Calculator,
    name = "calculate",
    description = "Calculate the result of a given formula.",
    args = [
        "formula" : {
            "type": "string",
            "description": "Numerical expression to compute the result of, in Python syntax.",
        }
    ]
)]
fn calculate(formula: &str) -> Result<f64> {
    match eval_str(formula) {
        Ok(result) => Ok(result),
        Err(err) => Err(err.into()),
    }
}

#[allow(unused)]
#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn test_calculate_add() {
        assert_eq!(
            Calculator::calculate("1 + 1").unwrap(),
            2.
        );

        assert_eq!(
            Calculator::calculate("1.1212 + 12.121").unwrap(),
            13.2422
        );
    }
}